package com.tgu.home_action.config;


import com.tgu.home_action.controller.filter.SocketInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Autowired
    private SocketInterceptor socketInterceptor;

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //注册一个断点 用于握手的地址
        registry.addEndpoint("/websocket")
                //允许websocket跨域
//                .setAllowedOrigins("*")
                .setAllowedOriginPatterns("*");
        //启用websocket备选方案(浏览器不支持的话就会启动
//                .withSockJS();
    }


    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 启用简单的消息代理，用于客户端订阅，进行广播的发送的前缀,如下面/agent将会推送经理相关的消息
        registry.enableSimpleBroker("/case", "/user");
        registry.setUserDestinationPrefix("/user");
        // 前端发送信息给后端的前缀
        registry.setApplicationDestinationPrefixes("/home");
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(socketInterceptor);
    }

}
